package com.yuanfuxing.boxuegu.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;

import android.os.Bundle;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yuanfuxing.boxuegu.R;
import com.yuanfuxing.boxuegu.activity.ExerciseModel;
import com.yuanfuxing.boxuegu.activity.VideoListActivity;
import com.yuanfuxing.boxuegu.activity.WenZiActivity;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/2.
 */

public class ExerciseFragment extends Fragment {

    private  RecyclerView recyclerView;
    public ArrayList<ExerciseModel> exercises = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment_exercise, container,false);
        recyclerView = view.findViewById(R.id.recycler);
        loading();
        return view;
    }
    public void loading(){
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd","7");
        params.addQueryStringParameter("v1","274");
        params.addQueryStringParameter("v2","1");
        params.addQueryStringParameter("v3","50");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("lucashu","result:"+result);
                Gson gson = new Gson();
                String result1 = convertString(result);
                Type listType = new TypeToken<ArrayList<ExerciseModel>>(){
                }.getType();
                if(gson == null ){
                    gson = new Gson();
                }

                exercises = gson.fromJson(result1,listType);
                GridLayoutManager manager = new GridLayoutManager(getActivity(),1);
                recyclerView.setLayoutManager(manager);
                recyclerView.setAdapter(new MyAdatper());

           }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
            }

        });

    }
    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");
        return result1;

    }
    public class MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(),R.layout.item_exercise,null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
//             ExerciseModel.Exercise exercise = exercises.get(position);
            final ExerciseModel  e = exercises.get(position);
            holder.textView.setText( e.contentTitle);
 //           holder.contentTitleTv.setText(exercise.websubitemID);
            holder.mRootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), WenZiActivity.class);
                    intent.putExtra("ID",e.contentID);
//                    intent.putExtra("v1",e.webname);
//                    intent.putExtra("v2",e.webitemname);
//                    intent.putExtra("v3",e.contentID);

                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return exercises.size();
        }
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        TextView contentTitleTv;
        LinearLayout mRootView;
        public MyViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.title1);
            contentTitleTv = itemView.findViewById(R.id.contenttitle1);
            mRootView = itemView.findViewById(R.id.rootview1);
        }
    }
}
